package com.lcy.attendance.system.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.lcy.attendance.system.util.CacheUtil;

/**
 * 检查session中的用户信息，如果没有，则导向到login页面
 * @author AsherYuan 2013-3-16
 */
public class SessionCheckInterceptor extends HandlerInterceptorAdapter {

	/**
	 * 在请求被dispatch之前调用
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();
		System.out.println("请求uri:" + uri);
		CacheUtil.getInstance().add("cacheTest", "cacheTest");
		if(uri == null || uri.equals("")) {
			response.sendRedirect("/index.do");
		} else {
			if(uri.equals("/login.do") || uri.equals("/index.do") || uri.indexOf("system") != -1) {
				return super.preHandle(request, response, handler);
			} else {
System.out.println("caceh:" + CacheUtil.getInstance().get("cacheTest"));
				HttpSession session = request.getSession();
				session.removeAttribute("username");
				if(session.getAttribute("username") == null) {
					response.sendRedirect("/system/sessionInvalid.do");
				} else {
					return super.preHandle(request, response, handler);
				}
			}
		}
		return false;
	}
}
